﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace Service
{
    /// <summary>
    /// Summary description for Consumer
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Consumer : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public string Authenticate(string accountno, int pin)
        {
            string response = "{response:{status:true,message:'Success'}}";
            if (string.IsNullOrEmpty(accountno)
                || pin <= 0)
            {
                //response = string.Format("consumeraccount:{0}, consumerpin:{1}, accountno:{2}, pin:{3}, amount:{4}", consumeraccount, consumerpin, accountno, pin, amount);
                response = "{response:{status:false,message:'invalid parameter'}}";
                return response;
            }
            try
            {
                var account = new Model.Account()
                {
                    AccountNumber = Convert.ToDouble(accountno),
                    PIN = pin
                };

                var commandAuthenticate = new AuthenticateUser(account);
                if (!commandAuthenticate.Execute())
                    response = string.Format("{response:{status:fail, message:'Invalid account no/ pin'}}");

            }
            catch (Exception ex)
            {
                response = "{response:{status:false,message:'" + ex.Message + "'}}";
            }
            return response;
        }

        [WebMethod]
        public string BalanceEnquiry(string accountno, int pin)
        {
            string response = "{response:{status:true,message:'Success'}}";
            if (string.IsNullOrEmpty(accountno)
                || pin <= 0)
            {
                //response = string.Format("consumeraccount:{0}, consumerpin:{1}, accountno:{2}, pin:{3}, amount:{4}", consumeraccount, consumerpin, accountno, pin, amount);
                response = "{response:{status:false,message:'invalid parameter'}}";
                return response;
            }
            try
            {
                var accountFrom = new Model.Account()
                {
                    AccountNumber = Convert.ToDouble(accountno),
                    PIN = pin
                };

                var commandCheckBalance = new CheckBalance(accountFrom);
                if (commandCheckBalance.Execute())
                    response = "{response:{status:true, message:'Account: " + accountno + " balance: " + commandCheckBalance.Balance + "'}}";

            }
            catch (Exception ex)
            {
                response = "{response:{status:false,message:'" + ex.Message + "'}}";
            }
            return response;
        }

        [WebMethod]
        public string BalanceTransfer(string accountfrom, int pin, string accountto, double amount)
        {
            string response = "{response:{status:true,message:'Success'}}";
            if (string.IsNullOrEmpty(accountfrom)
                || string.IsNullOrEmpty(accountto)
                || pin <= 0
            || amount <= 0)
            {
                //response = string.Format("consumeraccount:{0}, consumerpin:{1}, accountno:{2}, pin:{3}, amount:{4}", consumeraccount, consumerpin, accountno, pin, amount);
                response = "{response:{status:false,message:'invalid parameter'}}";
                return response;
            }
            try
            {
                var command = new BalanceTransfer(accountfrom, pin, accountto, 0, amount);

                var accountFrom = new Model.Account()
                {
                    AccountNumber = Convert.ToDouble(accountfrom),
                    PIN = pin
                };

                var commandCheckBalance = new CheckBalance(accountFrom);
                commandCheckBalance.Execute();
                if (commandCheckBalance.Balance < amount)
                {
                    response = "{response:{status:false,message:'Insufficient balance'}}";
                }
                else if (!command.Execute())
                    response = "{response:{status:false,message:'Action fail'}}";
            }
            catch (Exception ex)
            {
                response = "{response:{status:false,message:'" + ex.Message + "'}}";
            }
            return response;
        }

        [WebMethod]
        public string ChangePin(string accountno, int pin, int oldPin)
        {
            string response = "{response:{status:true,message:'Success'}}";
            if (string.IsNullOrEmpty(accountno)
                || pin <= 0)
            {
                //response = string.Format("consumeraccount:{0}, consumerpin:{1}, accountno:{2}, pin:{3}, amount:{4}", consumeraccount, consumerpin, accountno, pin, amount);
                response = "{response:{status:false,message:'invalid parameter'}}";
                return response;
            }
            try
            {
                var accountFrom = new Model.Account()
                {
                    AccountNumber = Convert.ToDouble(accountno),
                    PIN = pin
                };

                var commandChangePin = new ChangePin(accountFrom, oldPin);
                if (!commandChangePin.Execute())
                    response = string.Format("{response:{status:false, message:'Fail, account no/ pin wrong'");

            }
            catch (Exception ex)
            {
                response = "{response:{status:false,message:'" + ex.Message + "'}}";
            }
            return response;
        }

        [WebMethod]
        public string PayBill(string consumeraccount, int consumerpin, string accountno, int pin, double amount)
        {
            string response = "{response:{status:true,message:'Success'}}";
            if (string.IsNullOrEmpty(consumeraccount)
                || string.IsNullOrEmpty(accountno)
                || consumerpin <= 0
                || pin <= 0
            || amount <= 0)
            {
                //response = string.Format("consumeraccount:{0}, consumerpin:{1}, accountno:{2}, pin:{3}, amount:{4}", consumeraccount, consumerpin, accountno, pin, amount);
                response = "{response:{status:false,message:'invalid parameter'}}";
                return response;
            }
            try
            {
                var command = new BalanceTransfer(accountno, pin, consumeraccount, consumerpin, amount);

                var accountFrom = new Model.Account()
                {
                    AccountNumber = Convert.ToDouble(accountno),
                    PIN = pin
                };

                var commandCheckBalance = new CheckBalance(accountFrom);
                commandCheckBalance.Execute();
                if (commandCheckBalance.Balance < amount)
                {
                    response = "{response:{status:false,message:'Insufficient balance'}}";
                }
                else if (!command.Execute())
                    response = "{response:{status:false,message:'Action fail'}}";
            }
            catch (Exception ex)
            {
                response = "{response:{status:false,message:'" + ex.Message + "'}}";
            }
            return response;
        }

    }

}
